package com.opmsys.controller;

import com.opmsys.pojo.dto.RepairPageDTO;
import com.opmsys.pojo.entity.Repair;
import com.opmsys.result.PageResult;
import com.opmsys.result.Result;
import com.opmsys.service.RepairService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 描述
 *
 * @datatime:2024-06-18 12:30
 */
@RestController
@RequestMapping("/repair")
@Slf4j
public class RepairController {

    @Autowired
    RepairService repairService;

    /**
     * 新增维修记录
     *
     * @param repair
     * @return
     */
    @PostMapping
    public Result add(Repair repair){
        log.info("新增维修记录");
        if(repair.getRepairDate() == null){
            return Result.error("维修记录日期不能为null");
        }
        repairService.add(repair);
        return Result.successWithMsg("新增成功");
    }

    /**
     * 更新维修记录
     *
     * @param repair
     * @return
     */
    @PutMapping
    public Result update(Repair repair){
        log.info("更新维修记录");
        repairService.update(repair);
        return Result.successWithMsg("更新成功");
    }

    /**
     * 批量删除维修
     *
     * @param ids
     * @return
     */
    @DeleteMapping
    public Result delete(@RequestParam(required = false) List<Integer> ids){
        log.info("删除维修记录id："+ ids.toString());
        repairService.delete(ids);
        return Result.successWithMsg("删除成功");
    }

    /**
     * 维修分页查询
     * @param repairPageDTO
     * @return
     */
    @GetMapping("/page")
    public Result<PageResult> page(RepairPageDTO repairPageDTO){
        log.info("维修分页查询执行,{}",repairPageDTO);
        PageResult pageResult = repairService.pageQuery(repairPageDTO);
        return Result.success(pageResult);
    }

}
